<?php
namespace common\action;

use Yii;
use yii\base\Action;
use common\models\UploadForm;
use yii\helpers\ArrayHelper;
use yii\web\Response;
use yii\web\NotFoundHttpException;

/**
 * 上传独立控制器
 */
class UploadAction extends Action
{
    /**
     * @var array
     */
    public $config = [];

    /**
     * 初始化配置
     */
    public function init()
    {
        Yii::$app->request->enableCsrfValidation = false;
        Yii::$app->response->format = Response::FORMAT_JSON;
        //load config file
        if (!is_file(Yii::getAlias('@vendor') . '/kucha/ueditor/config.php')) {
            throw new NotFoundHttpException('请确认是否安装了kucha/ueditor扩展');
        }
        $_config = require(Yii::getAlias('@vendor') . '/kucha/ueditor/config.php');
        $this->config = ArrayHelper::merge($_config, $this->config);
        parent::init();
    }

    public function run()
    {
        $action = Yii::$app->request->get('action');
        switch ($action) {
            case 'config':
                $result = $this->config;
                break;
            default:
                $result = $this->upload();
                break;
        }
        return $result;
    }


    /**
     * 上传图片
     */
    public function upload()
    {
        $model = new UploadForm();
        return $model->upload(); // 上传图片
    }
}
